﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace Backend
{
    /* Brand Class
     * This class will handle the employee's specialties 
     * and brands that belongs to a customers scooter. 
     * It will contain the methods to create, remove and update brands */
    class Brand
    {
		DBConnect dbconn = new DBConnect();

        public bool CheckBrand(int bid)
        {
            int results;
            string query;

            query = dbconn.RunQueryReturn("SELECT COUNT(*) FROM brand WHERE bid = '" + bid + "'");
            results = Convert.ToInt32(query);

            if (results > 0)
                return true;
            else
                return false;
        }

		public bool Create(string brandName)
		{
			int results;
			string query;

			query = dbconn.RunQueryReturn("SELECT COUNT(*) FROM brand WHERE name = '" + brandName + "'");
			results = Convert.ToInt32(query);

			if (results > 0)
				return false;
			else
			{
				dbconn.RunQuery("INSERT INTO brand (name) VALUES ('" + brandName + "')");
				return true; 
			}
		}

		public bool Update(int bid, string brandName)
		{
            if (CheckBrand(bid) != true)
                return false;
            else
            {
                dbconn.RunQuery("UPDATE brand SET name = '" + brandName + "' WHERE bid = '" + bid + "' LIMIT 1");
                return true;
            }
		}

		public bool Remove(int bid)
		{
            if (CheckBrand(bid) != true)
                return false;
            else
            {
                dbconn.RunQuery("DELETE * FROM brand WHERE bid = '" + bid + "'");
                return true;
            }
		}

        /* ListBrands Method
         * This method will list all brands that matches the search word.
         * The input (string name) is optional. */
        public DataTable List(string name = "")
        {
            DataTable brandTable = dbconn.ReturnDataTable("brand", " ORDER BY name");                
            return brandTable; // Returns the DataTable
        }

        public string Find(string bid)
        {
            string brandName = dbconn.RunQueryReturn("SELECT name FROM brand WHERE bid = '" + bid + "'");
            return brandName;
        }
    }

    /* Scooter Class
     * This class will handle all the registrated scooters
     * and the methods to search, create, remove and update scooters */
    class Scooter
    {
		DBConnect dbconn = new DBConnect();

        public bool Check(string vin)
        {
            int results;
            string query;

            query = dbconn.RunQueryReturn("SELECT COUNT(*) FROM scooter WHERE vin = '" + vin + "'");
            results = Convert.ToInt32(query);

            if (results > 0)
                return true;
            else
                return false;
        }

		public bool Create(string vin, int cid, string registration, int bid)
		{
			int results;
			string query;

			query = dbconn.RunQueryReturn("SELECT COUNT(*) FROM scooter WHERE vin = '" + vin + "'");
			results = Convert.ToInt32(query);

			if (results > 0)
				return false;
			else
			{
				dbconn.RunQuery("INSERT INTO scooter (vin, cid, registration, bid) VALUES ('" + vin + "', '" + cid + "', '" + registration + "', '" + bid + "')");
				return true; 
			}
		}

		public bool Update(string vin, int cid, string registration, int bid)
		{
            if (Check(vin) != true)
                return false;
            else
            {
                string[] updateSet = new string[3];

                updateSet[0] = "cid = '" + cid + "'";
                updateSet[1] = "registration = '" + registration + "'";
                updateSet[2] = "bid = '" + bid + "'";

                dbconn.RunQuery("UPDATE scooter SET " + string.Join(", ", updateSet) + " WHERE vin = '" + vin + "' LIMIT 1");
                return true;
            }
		}

		public bool Remove(string vin)
		{
            if (Check(vin) != true)
                return false;
            else
            {
                dbconn.RunQuery("DELETE FROM scooter WHERE vin = '" + vin + "'");
                return true;
            }
		}

        public DataTable List(int cid)
        {
            DataTable scooterTable = dbconn.ReturnDataTable("scooter", " WHERE cid = '" + cid + "'");
            return scooterTable;
        }

        public string[] GetInfo(string vin)
        {
            string[] scooterInfo = dbconn.ReturnArray("SELECT vin, cid, registration, bid FROM scooter WHERE vin = '" + vin + "'", 4);
            return scooterInfo;
        }
    }
}
